//
//  EditFlightCustomerController.m
//  ELife
//
//  Created by xiaozhi on 11-7-15.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "EditFlightCustomerController.h"
#import "UIViewUtil.h"
#import "FlightExplainController.h"

@implementation EditFlightCustomerController
@synthesize nameTextField, phoneTextField, idTextField, idTypeButton, insuranceButton, _customerModel, birthdayButton;


- (id)initWithCustomerModel:(FlightCustomerModel *)model obj:(id)obj backAction:(SEL)action
{
    self = [super initWithNibName:@"EditFlightCustomerController" bundle:nil];
    if (self) {
        self._customerModel = model;
                
        _action = action;
        _obj = obj;
        
        _idTypeArray = [[NSArray alloc] initWithObjects:@"身份证", @"护照", nil];
        _insuranceArray = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
    }
    return self;
}

- (void)dealloc
{
    [birthdayButton release];
//    [_customerModel release];
    [_idTypeArray release];
    [_insuranceArray release];
    [nameTextField release];
    [phoneTextField release];
    [idTextField release];
    [idTypeButton release];
    [insuranceButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (_customerModel != nil) {
        [nameTextField setText:_customerModel._user_name];
        [phoneTextField setText:_customerModel._phone];
        [idTextField setText:_customerModel._certificate_num];
        [idTypeButton setTitle:_customerModel._certificate_type forState:UIControlStateNormal];
        [insuranceButton setTitle:_customerModel._insurance_number forState:UIControlStateNormal];
    }
    
    
    self.view.backgroundColor = [UIViewUtil getCommonBg];
    [UIViewUtil viewRoundFrame:[self.view viewWithTag:1]];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)save
{
    if ([nameTextField.text length] == 0
        || [phoneTextField.text length] == 0
        || [idTextField.text length] == 0
        || [idTypeButton.titleLabel.text length] == 0
        || [insuranceButton.titleLabel.text length] == 0
        || [birthdayButton.titleLabel.text isEqualToString:@"YYYY-MM-DD"]) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"请将信息填充完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        return;
    }
    
    if (_customerModel == nil) {
        _customerModel = [[FlightCustomerModel alloc] init];
    }
    _customerModel._user_name = nameTextField.text;
    _customerModel._certificate_type = idTypeButton.titleLabel.text;
    _customerModel._phone = phoneTextField.text;
    _customerModel._certificate_num = idTextField.text;
    _customerModel._insurance_price = insuranceButton.titleLabel.text;
    _customerModel._birthday = birthdayButton.titleLabel.text;
    
    [self.navigationController popViewControllerAnimated:YES];
    [_obj performSelector:_action withObject:_customerModel];
}

- (void)inputExplain
{
    FlightExplainController *nextControl = [[FlightExplainController alloc] initWithImageList:[NSArray arrayWithObjects:[UIImage imageNamed:@"explain_input.jpg"], nil]];
    
    [self.navigationController pushViewController:nextControl animated:YES];
    
    [nextControl release];
}

- (void)idExplain
{
    FlightExplainController *nextControl = [[FlightExplainController alloc] initWithImageList:[NSArray arrayWithObjects:[UIImage imageNamed:@"explain_card.jpg"], nil]];
    
    [self.navigationController pushViewController:nextControl animated:YES];
    
    [nextControl release];
}

- (void)inputFinished
{
    [nameTextField resignFirstResponder];
    [phoneTextField resignFirstResponder];
    [idTextField resignFirstResponder];
}

- (void)showSpinner:(NSArray *)spinnerList
{
    //　hide tabbar
    UIActionSheet *spinner = [[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    
    for (NSInteger index = 0; index < [spinnerList count]; index++) {
        [spinner addButtonWithTitle:[spinnerList objectAtIndex:index]];
    }
    [spinner addButtonWithTitle:@"cancel"];
    
    spinner.cancelButtonIndex = [spinnerList count];
    
    [spinner showInView:self.view];
    [spinner release];
    
}

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 110) {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        formatter.dateFormat = @"YYYY-MM-dd";
        
        UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:111];
        NSString *timestamp = [formatter stringFromDate:datePicker.date];
        
        [birthdayButton setTitle:timestamp forState:UIControlStateNormal];
    } else {
        if (buttonIndex < [_curArray count]) {
            NSString *text = [_curArray objectAtIndex:buttonIndex];
            [_listener setTitle:text forState:UIControlStateNormal];
        }
    }
}

- (void)pickBirthday
{
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:title 
                                  delegate:self 
                                  cancelButtonTitle:@"取消" 
                                  destructiveButtonTitle:@"设置"
                                  otherButtonTitles:nil];
    
    actionSheet.tag = 110;
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.tag = 111;
    datePicker.datePickerMode = UIDatePickerModeDate;
    [actionSheet addSubview:datePicker];
    [datePicker release];
    
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)action:(id)sender
{
    _listener = sender;
    // 选择证件类型
    if (sender == idTypeButton) {
        [self showSpinner:_idTypeArray];
        _curArray = _idTypeArray;
    } else if(sender == insuranceButton) {
        [self showSpinner:_insuranceArray];
        _curArray = _insuranceArray;
    }
}

@end
